%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     cowboy相关工具
%%% @end
%%%-------------------------------------------------------------------
-module(util_cowboy).

-export([
    must_post_val/2
]).

-include("common.hrl").
-include("http.hrl").
-include("error.hrl").


%%%===================================================================
%%% API
%%%===================================================================

must_post_val(Req, Keys) ->
    {ok, KeyValues, _Req2} = cowboy_req:read_urlencoded_body(Req),
    must_post_val(Keys, KeyValues, []).
must_post_val([], _KeyValues, Datas) ->
    lists:reverse(Datas);
must_post_val([Key | Keys], KeyValues, Datas) ->
    case proplists:get_value(Key, KeyValues) of
        undefined ->
            ?RESP_FAILED_ERR(?HTTP_STATUS_BAD_REQUEST, ?ERROR_PARAM);
        <<>> ->
            ?RESP_FAILED_ERR(?HTTP_STATUS_BAD_REQUEST, ?ERROR_PARAM);
        Val ->
            must_post_val(Keys, KeyValues, [Val | Datas])
    end.
